/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package IpChat;

/**
 *
 * @author lubinchen
 */



import java.net.InetAddress;
import java.util.*;



public class callthis {




   private int port=4111;
   private InetAddress tempIpAdd= null;
   private InetAddress ownIP=null;
   private String clientnamenow="";
   private int arrayIndex=0;

   public ArrayList<String> OnlineUserIP;
   public ArrayList<String> OnlineUserName;
   public ArrayList<Boolean> clientconnected;


   // temporary constructor
   public callthis(){
       OnlineUserIP= new ArrayList<String>(0);
       OnlineUserName=new ArrayList<String>(0);
       clientconnected= new ArrayList<Boolean>(0);
  
   }




   /*

   this is the proper and real constructor


    
    public callthis(ArrayList<String> PassedIn_OnlineIP;   ArrayList<String> PassedIn_OnlineName ){
       
    // initialize the attributes
       OnlineUserIP= new ArrayList<String>(0);
       OnlineUserName=new ArrayList<String>(0);
       clientconnected= new ArrayList<Boolean>(0);


     //pass in the IP address, construct clientconnected to be all false
      for (int i=0; i< PassedIn_OnlineIP.size();i++)
        {
             OnlineUserIP.add(PassedIn_OnlineIP.get(i));
             clientconnected.add(false);
         }



     // pass in username arraylist one by one
      for (int i=0; i< PassedIn_OnlineName.size();i++)
            OnlineUserName.add(PassedIn_OnlineName.get(i));


    // after all arrayList is done, start new server, assuming port=4000
    ChatServer server=new ChatServer(4000, this);


   }

    */











   public void setServer(int ports){

       port=ports;
       ChatServer server=new ChatServer(port, this);

   }





   public void findOwnIP(){
       try{
	      ownIP=InetAddress.getLocalHost();
	      System.out.println("IP of my own system is := "+ownIP.getHostAddress());

            }catch (Exception e){
	      System.out.println("Exception caught "+e.getMessage());
	  }
   }



   private void ConnectClients(){

        for (int i=0; i< OnlineUserIP.size();i++)
        {
            if(clientconnected.get(i)==false){
                getInet(OnlineUserIP.get(i));
                clientnamenow= OnlineUserName.get(i);
                arrayIndex=i;
                newClient();
            }
        }

        checkunconnected();
   }


   private void checkunconnected(){


       for (int i=0; i< OnlineUserIP.size();i++)
            if(clientconnected.get(i)==false)
                 ConnectClients();
            
              
   }



   public int findIndex(String IP){

       int  index=-1;
       for (int i=0; i< OnlineUserIP.size();i++)
            if(OnlineUserIP.get(i) == null ? IP == null : OnlineUserIP.get(i).equals(IP))
                index=i;

       return index;
    }


    public void newClient(){

    	    ChatClient client= new ChatClient(tempIpAdd, port, clientnamenow, this,arrayIndex );
    }



    public InetAddress getInet(String DestinationIP)
    {

    try{
        tempIpAdd= InetAddress.getByName(DestinationIP);

        }catch (Exception e){}

    return tempIpAdd;
    }










    public static  void main(String args[]){

        int portNo=4000;

        callthis call=new callthis();
        call.setServer(portNo);


        // read all input from clients and display them
        // a client can quit by typing ".quit"

        // Add all online people
        call.OnlineUserIP.add("172.17.142.205");
        call.OnlineUserName.add("WLwindows");
        call.clientconnected.add(false);

        call.OnlineUserIP.add("172.17.184.154");
        call.OnlineUserName.add("LubinMac");
        call.clientconnected.add(false);


        /*

        call.OnlineUserIP.add("172.20.6.149");
        call.OnlineUserName.add("SoramiPC");
        call.clientconnected.add(false);

        call.OnlineUserIP.add("172.17.189.197");
        call.OnlineUserName.add("SoramiMac");
        call.clientconnected.add(false);
*/


        call.checkunconnected();

    }



}

